package com.tuanzi.loan.business.repository.templateConf.impl;

import com.tuanzi.loan.business.repository.templateConf.custom.ActivityRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.templateConf.ActivityRequest;
import com.tuanzi.loan.vo.response.templateConf.ActivityResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_API_ACTIVITY;

@SuppressWarnings({"rawtypes"})
@Repository
public class ActivityRepositoryImpl extends JOOQBaseRepository implements ActivityRepositoryCustom {
    @Override
    public Page<ActivityResponse> list(ActivityRequest searchRequest, Pageable pageable) {
        fields(LOAN_API_ACTIVITY.ID, LOAN_API_ACTIVITY.API_NAME,
                LOAN_API_ACTIVITY.USER_NAME, LOAN_API_ACTIVITY.STATUS,
                LOAN_API_ACTIVITY.REMARK, LOAN_API_ACTIVITY.CTIME,
                LOAN_API_ACTIVITY.UPTIME, LOAN_API_ACTIVITY.THIRD_ID,
                LOAN_API_ACTIVITY.THIRD_NAME);
        eq(LOAN_API_ACTIVITY.ID, searchRequest.getId());
        contains(LOAN_API_ACTIVITY.API_NAME, searchRequest.getApiName());
        eq(LOAN_API_ACTIVITY.THIRD_ID, searchRequest.getThirdId());
        contains(LOAN_API_ACTIVITY.THIRD_NAME, searchRequest.getThirdName());
        orderBy(LOAN_API_ACTIVITY.ID, Sort.Direction.DESC);
        return search(pageable, record -> {

            ActivityResponse response = new ActivityResponse();
            response.setId(record.get(LOAN_API_ACTIVITY.ID));
            response.setApiName(record.get(LOAN_API_ACTIVITY.API_NAME));
            response.setUserName(record.get(LOAN_API_ACTIVITY.USER_NAME));
            response.setStatus(record.get(LOAN_API_ACTIVITY.STATUS));
            response.setUptime(record.get(LOAN_API_ACTIVITY.UPTIME));
            response.setThirdId(record.get(LOAN_API_ACTIVITY.THIRD_ID));
            response.setThirdName(record.get(LOAN_API_ACTIVITY.THIRD_NAME));
            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_API_ACTIVITY;
    }
}
